package com.fsy.controller;

import com.fsy.entities.Book;
import com.fsy.entities.Computer;
import com.fsy.service.ComputerService;
import com.fsy.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/computer")
public class ComputerController {
    //注入
    @Resource
    private ComputerService computerService;

    /**
     * 查询
     * @param computer
     * @return
     */
    @GetMapping("/findByComputer")
    public Map<String,Object> findByComputer(Computer computer){
        //查询全部用户信息
        List<Computer> computerList = computerService.findByComputer(computer);
        //查询全部用户数量
        int count = computerService.selectComputerCounts(computer);
        return Result.toClient("0", "",count,computerList);
    }

    //新增
    @PostMapping
    public Map<String,Object> add(@ModelAttribute Computer computer){
        boolean save = computerService.save(computer);
        Map<String,Object> map = new HashMap<>();
        if (save){
            map.put("code","0");
            map.put("msg","新增成功");
        }else {
            map.put("code","1");
            map.put("msg","新增失败");
        }
        return map;
    }

    //修改
    @PutMapping
    public Map<String,Object> update(@ModelAttribute Computer computer){
        boolean update = computerService.updateById(computer);
        Map<String,Object> map = new HashMap<>();
        if (update){
            map.put("code","0");
            map.put("msg","修改成功");
        }else {
            map.put("code","1");
            map.put("msg","修改失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/{id}")
    public Map<String,Object> del(@PathVariable("id") Integer id){
        boolean del = computerService.removeById(id);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/ComputerDel")
    public Map<String,Object> batchDel(List<Integer> list){
        boolean del = computerService.removeBatchByIds(list);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }
}
